# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 波士顿房价预测（5L2正则化）.py
# @Author: dongguangwen
# @Date  : 2025-02-06 14:22
import numpy as np
from sklearn.linear_model import LinearRegression, Ridge
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error

# 1.准备数据
np.random.seed(22)
x = np.random.uniform(-3, 3, size=100)
# print(x)
y = 0.5 * x ** 2 + np.random.normal(0, 1, size=100)
print(y)

# 2.模型训练
model = Ridge(alpha=0.01)  # 调整alpha 正则化强度 查看正则化效果
X = x.reshape(-1, 1)
X2 = np.hstack([X, X ** 2, X ** 3, X ** 4, X ** 5, X ** 6, X ** 7, X ** 8, X ** 9, X ** 10])  # 数据增加多次项
model.fit(X2, y)
print('权重参数值：', model.coef_)  # l2 正则不会将系数变为0 但是对高次方项系数影响较大

# 3.模型预测
y_pred = model.predict(X2)
print(y_pred)

# 4.计算均方误差
myret = mean_squared_error(y, y_pred)
print('myret-->', myret)

# 5.展示效果
plt.scatter(X, y)
# 画图plot折线图时 需要对x进行排序, 取x排序后对应的y值
plt.plot(np.sort(x), y_pred[np.argsort(x)], color='red')
plt.show()

"""
[ 2.68991269 -0.15896455 -1.0657469   1.77691179  2.2169277   1.44760438
  1.95468165  1.43909861  0.15853376  1.32780734  4.86511909  0.39702949
  2.63475246 -0.14604323  2.36460552  1.62061024  0.83996403  3.84213349
  0.38401779  1.60688722  1.07599777  0.65478898  0.9188096  -0.64424027
  1.59171645 -0.51865919  1.34359201  2.92620657  2.80123304  4.40659537
  0.87811453  1.10113724  6.1923925   0.855368    2.53145803  2.20814387
  0.50914933  3.56598319 -1.52802892 -0.22965108  0.68003709  4.97404497
 -0.75143267  2.88409744  5.60301458  0.99887944  2.62802974  2.46495793
  3.66571442 -1.85804906  0.83140672  0.86165142  0.33581663  0.06046012
  2.05133222  0.89686498  0.570795    4.81741428 -0.24435866  1.68542272
  2.49369326  2.83636823 -0.23086228  2.26822132  2.33381088  0.67749307
  1.59425991  2.65328755  3.70118035  3.9285876   1.95288399  2.60958529
  3.87293405  3.76537917  0.40451931  3.28686949  0.78030988  4.27240723
  2.19597273 -0.5814483   1.44486224  0.43852349 -0.27671756  2.04600828
  4.32094833 -0.20276307  2.15265487  1.69291982  0.10829686  5.84398733
  0.48910081  5.08461625  0.35116797  2.44363252  0.07344753  1.08868863
  1.41343863  2.96338708  4.35563861  1.45501813]
权重参数值： [ 4.78000150e-01  1.89735343e+00 -1.08376974e+00 -9.44111493e-01
  5.19464168e-01  2.43260692e-01 -8.97289127e-02 -2.59731835e-02
  5.02482481e-03  9.56247715e-04]
[ 1.88113673 -0.24526329  0.04561208  2.57854655  2.11905298  1.05682442
  1.68482056  0.85172924  1.84616019  1.81126076  3.87586932  0.14759348
  1.8392406   1.06432395  1.97099227  3.45009302  1.28593561  2.99419576
  0.8814672   1.50120725  1.24572359  0.84424088  0.42092626  0.54715097
 -0.01712149  0.32853713  0.88346067  3.32648587  3.02449892  3.89112492
  0.81509237  1.56358214  4.91586894  2.05547255  2.7899833   1.06325968
 -0.18939578  2.87085357 -0.24431217 -0.15569746  1.37372485  5.09541256
 -0.0952452   3.83489061  5.09717374  0.83523471  2.42357927  2.95089218
  2.85264665 -0.24075098  1.92493433  1.39114188  0.49658953 -0.13309891
  3.51810762  0.90232273  1.85054107  2.15969755 -0.01912655  1.35164437
  3.02979288  4.15228781  1.34485297  1.84946484  1.01521781  2.08135134
  1.06094638  2.0574261   2.56767715  3.38235828  0.81153247  2.97579685
  2.97863871  4.69530827  0.80262306  2.20056545  0.20200826  1.79202248
  2.28584316 -0.20586548  0.23089197 -0.23618378 -0.1890929   1.23900107
  2.90867446  1.76290644  2.62106575  0.65905351  0.86833862  4.95295791
  0.75234336  4.7738364  -0.19530894  3.00224804 -0.11621207  1.91366162
  1.6876414   1.74506697  4.94414385  1.49863032]
myret--> 0.81448033862415
"""
